package ar.org.cfired.creditos.guias;

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.util.List;

import ar.org.cfired.hibernate.utils.HibernateUtil;
import ar.org.cfired.creditos.guias.RespuestaGuia;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SQLQuery;

public class ServicioAjax extends HttpServlet {

	  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Logger objetoLogger = Logger.getLogger(this.getClass().getName());
	public void init() throws ServletException {
		    // Initialization here...
		  }

		  public void service( HttpServletRequest req, HttpServletResponse res ) 
		        throws ServletException 
		  {
		    try
		    {
		        String cre_id = req.getParameter("cre_id");
		        String gui_id = req.getParameter("gui_id");
		        String sec_id = req.getParameter("sec_id");
		        String pre_numero = req.getParameter("pre_numero");
		        String tab_columna = req.getParameter("tab_columna");
		        String tab_numero_orden = req.getParameter("tab_numero_orden");
		        String valor = req.getParameter("valor");
		        String sesion = req.getParameter("sesion");
		        String usuario = req.getParameter("usuario");
		        String pagina = req.getParameter("pagina");
		    	System.err.println(valor);


		    	/*String sql = "exec dbo.guardar_datos @cre_id=" + cre_id + ", @gui_id=" + gui_id + ", ";
		    	sql += "@sec_id=" + sec_id + ", @pre_numero=" + pre_numero + ", ";
		    	sql += "@tab_columna=" + tab_columna + ", @tab_numero_orden=" + tab_numero_orden + ", ";
		    	sql += "@valor = " + valor + ", @usuario='" + usuario + "', ";
		    	sql += "@sesion = '" + sesion + "'";*/
		    	//objetoLogger.debug(sql);
		    	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				Transaction t = session.beginTransaction();
				//SQLQuery q = session.createSQLQuery(sql);
				try {
			        if (valor != null) valor = new String(valor.getBytes("windows-1252"),"UTF8");
			        if (sesion != null) sesion = new String(sesion.getBytes("windows-1252"),"UTF8");
			        if (usuario != null) usuario = new String(usuario.getBytes("windows-1252"),"UTF8");
			        if (pagina != null) pagina = new String(pagina.getBytes("windows-1252"),"UTF8");
			        System.err.println(valor);
			        
					SQLQuery q = (SQLQuery)session.getNamedQuery("sp_guardar_datos");
					q.setInteger("cre_id", Integer.valueOf(cre_id));
					q.setInteger("gui_id", Integer.valueOf(gui_id));
					q.setInteger("sec_id", Integer.valueOf(sec_id));
					q.setInteger("pre_numero", Integer.valueOf(pre_numero));
					
					q.setString("valor", valor.replace("'", ""));
					q.setInteger("tab_columna", (tab_columna.equals("null"))?0:Integer.valueOf(tab_columna));
					q.setInteger("tab_numero_orden", (tab_numero_orden.equals("null"))?0:Integer.valueOf(tab_numero_orden));
					q.setString("usuario", usuario.replace("'", ""));
					q.setString("sesion", sesion);
					
					List<RespuestaGuia> rta = q.list();
					//System.err.println("Rta: " + rta.get(0).getSecId());
					
					t.commit();
					
				}
				catch (HibernateException e){
					System.err.println(e.toString());
				}		
				catch (UnsupportedEncodingException e) {
					System.err.println(e.toString());
				}
		    	// Write the response
		       	 res.setContentType("text/html; charset=iso-8859-1"); 
		     PrintWriter out = res.getWriter();
		     out.println("OK");

		    }
		    
		    catch( IOException ioe )
		    {
		     throw new ServletException( ioe );
		    }
		  }
		  public void destroy() {
		    // Cleanup here...
		  }
}	
	

